//
//  EnterpriseViewController.m
//  XTCloud
//
//  Created by QA on 2018/2/26.
//  Copyright © 2018年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "EnterpriseViewController.h"
#import "XLSlideSwitch.h"
#import "EnterpriseItemController.h"
#import "UIImage+Color.h"

#import "GETableView.h"
#import "RTSpinKitView.h"
#import "GetUserStationService.h"
#import "UIView+AssistView.h"

@interface EnterpriseViewController ()<XLSlideSwitchDelegate>

@property (nonatomic,strong) XLSlideSwitch *slideSwitch;

@end

@implementation EnterpriseViewController

- (id)init
{
    self = [super init];
    if (self) {
        //添加菜单权限获取注册
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMenuAuthorityService:)
                                                     name:GetUserBasicInfoServiceKey object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //添加进度条
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt];
    spinner.center = CGPointMake(SuperWidth / 2, CGRectGetMidY(self.view.frame));
    [spinner startAnimating];
    [self.view addSubview:spinner];
    
    //获取菜单数据
    [self initMainView];
}

- (void)initMainView
{
    //获取主模块
    NSPredicate *predicateMain = [NSPredicate predicateWithFormat:@"parentId = %@ and moduleType = %@", @0, @6];
    ModuleAuthorityEntity *mainEntity = [ModuleAuthorityEntity MR_findFirstWithPredicate:predicateMain sortedBy:@"order" ascending:YES];
    
    //获取菜单
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parentId = %@", mainEntity.id];
    NSArray *moduleArray = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:predicate];
    
    //移除进度条
    if(moduleArray.count <= 0){
        self.view.backgroundColor = [UIColor whiteColor];
        [self.view showPromptViewText:@"暂无模块" rect:CGRectMake(0, 0, SuperWidth, SuperHeight - 128) blcok:nil];
    }
    
    //移除进度条
    NSArray *viewArray = self.view.subviews;
    for (id view in viewArray) {
        if ([view isKindOfClass:[RTSpinKitView class]]) {
            RTSpinKitView *mSpinKitView = view;
            [mSpinKitView removeFromSuperview];
        }
    }
    
    //无数据直接返回
    if(moduleArray.count <= 0) return;
    
    //要显示的站点名称
    NSArray *stationArray = [[GetUserStationService sharedService] getAllStationToArray];
    NSMutableArray *titles = [NSMutableArray array];
    for(int i=0;i<stationArray.count;i++){
        StationInfoEntity *entity = [stationArray objectAtIndex:i];
        [titles addObject:entity.stationName];
    }
    
    //创建需要展示的ViewController
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    for (NSInteger i = 0 ; i<titles.count; i++) {
        StationInfoEntity *entity = [stationArray objectAtIndex:i];
        
        UIViewController *vc = [[EnterpriseItemController alloc] initWithStationId:entity.stationId menuArray:moduleArray];
        [viewControllers addObject:vc];
    }
    
    //如果创建就删除该控件
    if(_slideSwitch)[_slideSwitch removeFromSuperview];
    
    //创建滚动视图
    _slideSwitch = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, SuperWidth, self.view.bounds.size.height - 44) Titles:titles viewControllers:viewControllers];
    
    //隐藏站点栏
    if(stationArray.count<=1){
        [_slideSwitch setSegmentedHidden:YES];
    } else {
        [_slideSwitch setSegmentedHidden:NO];
    }
    
    //设置代理
    _slideSwitch.delegate = self;
    //设置按钮选中和未选中状态的标题颜色
    _slideSwitch.itemSelectedColor = ThemeColorBlue;
    _slideSwitch.itemNormalColor = [UIColor darkGrayColor];
    //标题横向间距
    _slideSwitch.customTitleSpacing = 20;
    //更多按钮
    //_slideSwitch.moreButton = [self moreButton];
    //显示方法
    [_slideSwitch showInViewController:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
    
    //去除黑色阴影线
    UIImage *image = [UIImage imageWithColor:[UIColor colorWithWhite:0.9 alpha:1] size:CGSizeMake(SuperWidth, 0.5)];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6){
        [self.navigationController.navigationBar setShadowImage:image];
    }
    
    //状态栏字体改变
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColorDispose colorWithHexString:@"#333333"], NSForegroundColorAttributeName,
                                                                     [UIFont systemFontOfSize:18.0], NSFontAttributeName, nil]];
}

- (void)dealloc
{
    //移除菜单权限获取注册
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //[self.navigationController.navigationBar lt_reset];
}

#pragma mark -
#pragma mark - NSNotificationCenter

-(void)updateMenuAuthorityService:(NSNotification *)notification
{
    //获取菜单权限列表
    if ([[notification name] isEqualToString:GetUserBasicInfoServiceKey]){
        [self initMainView];
    }
}

@end
